package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Menu;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.GoodsTypeService;
import com.atguigu.jxc.service.LogService;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author gwk
 * @create 2021 -09 -05 13:56
 */
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {
    @Autowired
    LogService logService;
    @Autowired
    GoodsTypeDao goodsTypeDao;
    @Override

    public String loadGoodsType() {

        return this.getAllGoodsType(-1).toString();
    }

    /**
     * 新增分类
     * @param goodsTypeName
     * @param pId
     * @return
     */
    @Override
    public ServiceVO save(String goodsTypeName, Integer pId) {
        GoodsType goodsType = new GoodsType();
        goodsType.setGoodsTypeName(goodsTypeName);
        goodsType.setPId(pId);
        if (pId == -1 || pId == 1){
            goodsType.setGoodsTypeState(1);
        }else {
            goodsType.setGoodsTypeState(0);
        }
        goodsTypeDao.save(goodsType);
        logService.save(new Log(Log.INSERT_ACTION,"新增分类："+goodsType.getGoodsTypeName()));
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除分类
     * @param goodsTypeId
     * @return
     */
    @Override
    public ServiceVO delete(Integer goodsTypeId) {
        goodsTypeDao.delete(goodsTypeId);
        logService.save(new Log(Log.DELETE_ACTION,"删除分类"));
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 递归查询所有菜单
     * @return
     */
    public JsonArray getAllGoodsType(Integer parentId){

        JsonArray array = this.getGoodsTypeByParentId(parentId);

        for(int i=0;i<array.size();i++){

            JsonObject obj = (JsonObject) array.get(i);

            if(obj.get("state").getAsString().equals("open")){//如果是叶子节点，不再递归

                continue;

            }else{//如果是根节点，继续递归查询

                obj.add("children", this.getAllGoodsType(obj.get("id").getAsInt()));

            }

        }

        return array;
    }

    /**
     * 根据父菜单ID获取所有子菜单
     * @return
     */
    public JsonArray getGoodsTypeByParentId(Integer parentId){

        JsonArray array = new JsonArray();

        List<GoodsType> GoodsTypeList = goodsTypeDao.selectTypeByParentId(parentId);

        //遍历菜单

        for(GoodsType goodsType : GoodsTypeList){

            JsonObject obj = new JsonObject();

            obj.addProperty("id", goodsType.getGoodsTypeId());//菜单ID

            obj.addProperty("text", goodsType.getGoodsTypeName());//菜单名称

            obj.addProperty("iconCls", "goods-type");//图标

            if(goodsType.getGoodsTypeState() == 1){

                obj.addProperty("state", "closed"); //根节点

            }else{

                obj.addProperty("state", "open");//叶子节点
            }


            JsonObject attributes = new JsonObject(); //扩展属性

            attributes.addProperty("state", goodsType.getGoodsTypeState());

            obj.add("attributes", attributes);

            array.add(obj);


        }

        return array;
    }
}

